/**
 * @package: BrickCAD
 * @className: Brick.java
 * @package: default
 * @author: Sean O'Brien, Jack, Lester Tam, Erik Telstad
 * @date_created: November 16, 2009
 * @last_motified: November 16, 2009
 * 
 * 
 * 
 */
import java.io.File;
import java.io.Serializable;
import java.util.Arrays;
import java.util.Observable;
import java.util.Stack;

public class Brick extends Observable implements Serializable, BrickMemento {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private File file;
	
	private String name;
	private double width;
    private double height;
    private double length;
    private BrickMemento state;
    private Stack<BrickMemento> undoStack;
    private Stack<BrickMemento> redoStack;
    
	private boolean changed = false;


    
    /**
     * default constructor. All fields are initialized to zero
     */
    Brick(){
    	name = "";
        height = 50;
        length = 100;
        width = 50;
        
        setChanged();
        clearChanged();
    }
    
    /**
	 * @return the height
	 */
	public double getHeight() {
		return height;
	}

	/**
	 * @param height the height to set
	 */
	public void setHeight(double height) {
		this.height = height;
		setChanged();
		clearChanged();
	}

	/**
	 * @return the length
	 */
	public double getLength() {
		return length;
	}

	/**
	 * @param length the length to set
	 */
	public void setLength(double length) {
		this.length = length;
		setChanged();
		clearChanged();
	}

	/**
	 * @return the width
	 */
	public double getWidth() {
		return width;
	}

	/**
	 * @param width the width to set
	 */
    public void setWidth(double width) {
    	this.width = width;
		setChanged();
		clearChanged();
    }
    
	public File getFile() {
		return file;
	}

	public void setFile(File newFile) {
		file = newFile;
	}

	public boolean isChanged() {
		return changed;
	}

	public void setChanged(boolean changed) {
		this.changed = changed;
	}

	/**
	 * notifies observers of changes and sets changed flag to true
	 */
	public void setChanged() {
		super.setChanged();
		this.notifyObservers();
		this.changed = true;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "Brick [height=" + height + ", length=" + length + ", width=" + width
				+ ", state=" + state + "]";
	}
	
	/* Gets the state from the undoStack()
	 * (non-Javadoc)
	 * @see BrickMemento#getState(double)
	 */
	public double getState(double d) {
		return d;
	}
	
	public double setState(double d) {
		return d;
	}
	
	/* Pops an element from the UndoStack
	 * 
	 */
	public void getUndoStack() {
		undoStack.pop();
	}

	/* Pops an element from the RedoStack
	 * 
	 */
	public void getRedoStack() {
		redoStack.pop();
	}

	/* If something is changed (isChanged flag is true),
	 * creates a BrickMemento of a value from Height, Length, or Width
	 * and stores it in the UndoStack
	 */
	public void makeMemento(double d) {
		 if(isChanged() == true) 
		undoStack.push(state);
	
	}
	
	/* 'Undos' an action by pushing the current state into 
	 * the RedoStack and popping the element from the UndoStack.
	 */
	public void restore(BrickMemento m) {
		redoStack.push(state);
		state = undoStack.pop();
	
	}

	/* Inherited from BrickMemento
	 * (non-Javadoc)
	 * @see BrickMemento#redoStack()
	 */
	@Override
	public Stack<BrickMemento> redoStack()
	{
		
		return redoStack;
	}

	/* Inherited from BrickMemento
	 * (non-Javadoc)
	 * @see BrickMemento#undoStack()
	 */
	@Override
	public Stack<BrickMemento> undoStack()
	{
		return undoStack;
	}

}
